/**
 * @(#)NoneedLoginUserServiceImpl.java 2017年5月22日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.INewClientInfoDao;
import org.jsbd.boss.domian.channel.*;
import org.jsbd.boss.enums.DataCheckStatusEnum;
import org.jsbd.boss.enums.HistoryUserTableEnum;
import org.jsbd.boss.enums.LoginTypeEnum;
import org.jsbd.boss.enums.TaskStatusEnum;
import org.jsbd.boss.util.BeanCopierUtil;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.VersonCompareUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 新历史用户
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service("historyNewUserService")
public class HistoryNewUserServiceImpl extends ThirdLoginAdapterHandler<NewUser> {
    public static final Logger LOGGER = LoggerFactory.getLogger(HistoryNewUserServiceImpl.class);


    @Resource
    private INewClientInfoDao newClientInfoDao;

    @Override
    public boolean init() {
        loginIndexKey = CacheKey.NEW_HISTORY_USER_LOGIN_INDEX;
        loginType = LoginTypeEnum.HISTORY_NEW_LOGIN;
        queueKey = CacheKey.NEW_HISTORY_USER_QUEUE;
        return true;
    }


    @Override
    public List<NewUser> selectUserList(ThirdUserQueryVo queryVo) {
        Integer start = 0;
        String indexKey = getIndexKey(queryVo.getBase(), queryVo.getApkId(), queryVo.getProvinceId(), queryVo.getCarrier());
        Object indexObj = redisClient.get(indexKey);
        if (indexObj != null) {
            start = Integer.parseInt(indexObj.toString());
        }
        queryVo.setStart(start);
        QueryUserClientVo userVo = new QueryUserClientVo();
        userVo.setId((long) queryVo.getStart());
        userVo.setLength(queryVo.getLength());
        userVo.setProvince(queryVo.getProvinceId());
        userVo.setCarrier(queryVo.getCarrier());
        List<UserClientInfo> clientList = newClientInfoDao.selectClientList(userVo);
        if (clientList != null && clientList.size() > 0) {
            List<NewUser> userList = new ArrayList<>();
            for (UserClientInfo clientInfo : clientList) {
                Long id = clientInfo.getId();
                if (id != null) {
                    if (start < clientInfo.getId().intValue()) {
                        start = clientInfo.getId().intValue();
                    }
                }
                String sdkVersion = clientInfo.getSdkVersion();
                String bizId = queryVo.getBizId();
                if (StringUtils.isNotBlank(bizId)) {
                    if (StringUtils.isBlank(sdkVersion) || (StringUtils.isNotBlank(sdkVersion) && VersonCompareUtil.compareVersion(sdkVersion, bizId) < 0)) {
                        continue;
                    }
                }
                clientInfo.setProvince(queryVo.getProvinceId());
                clientInfo.setTableName(queryVo.getTableName());
                NewUser thirdUser = new NewUser();
                BeanCopierUtil.UserClient2ThirdUserBeanCopier.copy(clientInfo, thirdUser, null);
                thirdUser.setId(clientInfo.getId().intValue());
                thirdUser.setLoginType(loginType.getType());
                thirdUser.setProvince(queryVo.getProvinceId());
                userList.add(thirdUser);
            }
            redisClient.put(indexKey, start);
            return userList;
        } else {
            //跑完了重置
            redisClient.put(indexKey, 0);
            LOGGER.warn("apk {} base {} carrier {} 省份 {} 登录类型 {} 用户不够 当前下标 {}", new Object[]{queryVo.getApkId(), queryVo.getBase(), queryVo.getCarrier(), queryVo.getProvinceId(), loginType.getName(), start});
            return null;
        }

    }

}
